import random
from pygame import draw , image , transform , mixer
from pygame.transform import smoothscale
from pygame.surface import Surface
from config import win_size
from pygame.locals import Rect


def _out_of_screen ( rect ) :
	return True if rect.x >= win_size [ 0 ] and rect.y >= win_size [
		1 ] and rect.x + rect.width <= 0 and rect.y + rect.height <= 0 else False


def _set_size ( surface , size ) :
	return smoothscale ( surface , size )


def _get_center ( rect ) :
	return float ( (win_size [ 0 ] / 2 - rect.centerx) ) , float ( (win_size [ 1 ] / 2 - rect.centery) )


def _get_center_x ( width ) :
	return float ( (win_size [ 0 ] - width) / 2 )


def _get_center_y ( height ) :
	return float ( (win_size [ 1 ] - height) / 2 )


def _get_bottom ( height ) :
	return float ( win_size [ 1 ] - height )


def _create_rect ( color , size , border = 0 ) :
	surface = Surface ( size ).convert ( )
	draw.rect ( surface , color , Rect ( [ 0 , 0 ] , size ) , border )
	return surface


def _create_image ( filename ) :
	img = image.load ( filename ).convert ( )
	return img


def _create_image_alpha ( filename ) :
	img = image.load ( filename ).convert_alpha ( )
	return img


def _change_size ( surface , size ) :
	return transform.smoothscale ( surface , size )


def _random_pos ( width , y ) :
	randx = random.randint ( 0 , win_size [ 0 ] )
	randy = random.randint ( y , 0 )
	return [ randx , randy ]


def _out_screen ( rect ) :
	return True if rect.x <= 0 or rect.y <= 0 or rect.x >= win_size [ 0 ] or rect.y >= win_size [ 1 ] else False


def _out_screen_bottom ( rect ) :
	return True if rect.y >= win_size [ 1 ] else False


def _create_font ( text , color , font ) :
	return font.render ( text , True , color ).convert_alpha ( )


def _get_right_bottom ( rect ) :
	return win_size [ 0 ] - rect.width , win_size [ 1 ] - rect.height


def _create_sound ( filename ) :
	return mixer.Sound ( filename )


def _play_bgm ( filename ) :
	mixer.music.load ( filename )
	mixer.music.play ( -1 )


def _cutImg ( surface , width = 32 , height = 32 , type = 0 ) :
	images = [ ]
	imgRect = surface.get_rect ( )
	col = imgRect.width / width
	row = imgRect.height / height
	if type == 0 :
		for x in range ( row ) :
			for y in range ( col ) :
				rect = Rect ( y * width , x * height , width , height )
				images.append ( surface.subsurface ( rect ) )
	else :
		for x in range ( col ) :
			for y in range ( row ) :
				rect = Rect ( x * width , y * height , width , height )
				images.append ( surface.subsurface ( rect ) )
	return images
